package com.qiaotongtianxia.loans.fragments;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.apkfuns.logutils.LogUtils;
import com.lzy.okgo.OkGo;
import com.qiaotongtianxia.loans.functionInterface.FragmentListener;
import com.qiaotongtianxia.loans.net.Api;

import java.util.Map;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * lisen
 */

@SuppressWarnings("all")
public abstract class BaseFragment extends Fragment {
    public Context context;

    public FragmentListener fragmentListener;

    public Map<String,Object> map;

    private String title;
    private Unbinder unbinder;

    public Api api;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onAttach(Activity context) {
        fragmentListener = (FragmentListener) context;
        this.context = context;
        Bundle arguments = getArguments();
        if (arguments != null) {
            map = (Map<String, Object>) arguments.getSerializable("map");
        }
        super.onAttach(context);
    }
    public void onReceive(String action,Intent intent){
        LogUtils.e("onReceive");

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(getLayoutId(),container,false);
        unbinder = ButterKnife.bind(this,view);
        if (api == null){
            api = onApiCreate();
        }
        created(view);
        initTitleLayout();
        return view;
    }

    /**
     *  页面布局文件的ID
     * @return
     */
    public abstract @LayoutRes int getLayoutId();

    /**
     * 控件初始化完成
     * @param view
     */
    public abstract void created(View view);

    public abstract Api onApiCreate();

    public void initTitleLayout(){}

    @Override
    public void onDestroyView() {
        if (unbinder != null){
            unbinder.unbind();
        }
        if (api != null){
            OkGo.getInstance().cancelTag(api);
        }
        super.onDestroyView();
    }

    public void permissionSuccess(int requestCode) {

    }

    public void permissionFail(int requestCode) {

    }
}
